/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RainPaintFrame.java
 *
 * Created on 9 Ιουλ 2009, 12:08:14 μμ
 */

package gui;

import utilities.DrawingBean;
import simulation.GUIOptionManager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * The frame used to define the rain conditions
 * @author Arvanitis Ioannis
 */
public class RainPaintFrame extends javax.swing.JFrame implements Runnable {
    private int terrain_width;
    private int terrain_height;
    private Vector<DrawingBean> v;
    private Thread runner;

    /**
     * Sets the vector with the rain details
     * @param v Vector with the rain details
     */
    public void setV(Vector<DrawingBean> v) {
        this.v = v;
    }

    /**
     * Gets vector with the rain details
     * @return the vector with the rain details
     */
    public Vector<DrawingBean> getV() {
        return v;
    }

    /**
     * Creates new form rainPaintFrame
     * @param w Width (in cells) of canvas
     * @param h Height (in cells) of canvas
     */
    public RainPaintFrame(int w, int h) {
        this.terrain_width = w;
        this.terrain_height = h;
        runner = new Thread(this);
        runner.start();
    }

    public void run() {
        setTitle("Rain conditions");
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screensize = toolkit.getScreenSize();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        //----------------------------------------------------------------------
        RainCanvasPanel canvas = new RainCanvasPanel(terrain_width,terrain_height);
        JPanel actions = new JPanel();

        final JRadioButton heavyRain = new JRadioButton("Heavy rain", true);
        final JRadioButton lightRain = new JRadioButton("Light rain", false);
        final JRadioButton clearArea = new JRadioButton("Clear space", false);
        ButtonGroup color = new ButtonGroup();
        color.add(heavyRain);
        color.add(lightRain);
        color.add(clearArea);
        RainCanvasPanel.setColor(new Color(0, 0, 128));
        RainCanvasPanel.setAttenuation(GUIOptionManager.getHeavyAttenuation());
        JButton ok = new JButton("Ok");
        JButton cancel = new JButton("Cancel");
        JButton clearAll = new JButton("Clear all");


        heavyRain.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (heavyRain.isSelected()) {
                    RainCanvasPanel.setColor(new Color(0, 0, 128));
                    RainCanvasPanel.setAttenuation(GUIOptionManager.getHeavyAttenuation());
                }
            }
        });
        lightRain.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (lightRain.isSelected()) {
                    RainCanvasPanel.setColor(new Color(65, 105, 225));
                    RainCanvasPanel.setAttenuation(GUIOptionManager.getLightAttenuation());
                }
            }
        });
        clearArea.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (clearArea.isSelected()) {
                    RainCanvasPanel.setColor(Color.WHITE);
                }
            }
        });
        ok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                GUIOptionManager.setRain(RainCanvasPanel.getV());
                dispose();
            }
        });
        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                RainCanvasPanel.getV().removeAllElements();
                dispose();
            }
        });
        clearAll.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                RainCanvasPanel.getV().removeAllElements();
                repaint();
            }
        });
        
        
        actions.setLayout(new GridLayout(1, 6));

        actions.add(heavyRain);
        actions.add(lightRain);
        actions.add(clearArea);
        actions.add(ok);
        actions.add(cancel);
        actions.add(clearAll);
        
        getContentPane().add(canvas, BorderLayout.CENTER);
        getContentPane().add(actions, BorderLayout.SOUTH);

        //----------------------------------------------------------------------

        setSize(canvas.getSize());
        int x = (int) ( screensize.getWidth() - getWidth() ) / 2;
        int y = (int) ( screensize.getHeight() - getHeight() ) / 2;
        setLocation( x, y );
        setVisible(true);
    }








    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
